package com.example.stumanager.student;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.stumanager.R;
import com.example.stumanager.database.UserSql;
import com.example.stumanager.entity.UserEntity;

/**
 * 学生注册
 */
public class StudentRegisterPage extends AppCompatActivity {

    Button back,submit;
    EditText number,name,passWord,confirmPassword,dormitory,grade;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_register_page);
        back= findViewById(R.id.back);//返回
        submit= findViewById(R.id.submit);//提交
        //学号
        number= findViewById(R.id.number);
        //姓名
        name= findViewById(R.id.name);
        //密码
        passWord= findViewById(R.id.passWord);
        //确认密码
        confirmPassword= findViewById(R.id.confirmPassword);
        //宿舍信息
        dormitory= findViewById(R.id.dormitory);
        //班级信息
        grade = findViewById(R.id.grade);
        submit.setOnClickListener(v -> {
            if(("").equals(passWord.getText().toString())|| ("").equals(confirmPassword.getText().toString())
                    ||("").equals(number.getText().toString())|| ("").equals(dormitory.getText().toString())
                    || ("").equals(grade.getText().toString())){
                Toast.makeText(StudentRegisterPage.this, "信息不能为空", Toast.LENGTH_SHORT).show();
                return ;
            }else {
                if (!(passWord.getText().toString().equals(confirmPassword.getText().toString()))){
                    Toast.makeText(StudentRegisterPage.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                }
            }
            UserSql u=new UserSql(getBaseContext());
                UserEntity userEntity =new UserEntity(number.getText().toString(),name.getText().toString(),passWord.getText().toString(),dormitory.getText().toString(),grade.getText().toString(),1);
                if(u.add(userEntity)){
                    Toast.makeText(StudentRegisterPage.this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else{
                    Toast.makeText(this, "注册失败，学号已存在", Toast.LENGTH_SHORT).show();
                }
        });

        back.setOnClickListener(v -> finish());
    }
}